<script setup lang="ts">
import { onMounted, ref } from "vue";
import ReCol from "@/components/ReCol";
import { formRules } from "./utils/rule";
import { FormProps, usePublicHooks } from "@/views/hooks";
import {
  getMenuList,
  getPermissionGroups,
  getPermissionMenu,
  Permission
} from "@/api/system";
import { handleTree } from "@/utils/tree";

const props = withDefaults(defineProps<FormProps<Permission>>(), {
  formInline: () => ({
    id: "",
    name: "",
    code: "",
    description: "",
    state: true
  })
});
const ruleFormRef = ref();
const { switchStyle } = usePublicHooks();
const newFormInline = ref(props.formInline);
const disabled = !!newFormInline.value.id;
const groupOptions = ref([]);

function getRef() {
  return ruleFormRef.value;
}

onMounted(() => {
  getPermissionGroups().then(res => {
    groupOptions.value = res;
  });
});

defineExpose({ getRef });
</script>

<template>
  <el-form
    ref="ruleFormRef"
    :model="newFormInline"
    :rules="formRules"
    label-width="82px"
  >
    <el-row :gutter="30">
      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="权限名称" prop="name">
          <el-input
            v-model="newFormInline.name"
            clearable
            placeholder="请输入权限名称"
          />
        </el-form-item>
      </re-col>
      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item v-if="!disabled" label="权限编码" prop="code">
          <el-input
            v-model="newFormInline.code"
            clearable
            placeholder="请输入权限编码"
          />
        </el-form-item>
        <el-form-item v-if="disabled" label="权限编码">
          <el-input
            v-model="newFormInline.code"
            clearable
            placeholder="请输入权限编码"
            :disabled="disabled"
          />
        </el-form-item>
      </re-col>
      <re-col>
        <el-form-item label="权限描述" prop="description">
          <el-input
            v-model="newFormInline.description"
            clearable
            placeholder="请输入权限描述"
          />
        </el-form-item>
      </re-col>
      <re-col>
        <el-form-item label="权限分组" prop="group">
          <el-select
            v-model="newFormInline.group"
            filterable
            allow-create
            default-first-option
            placeholder="请选择或输入权限分组"
          >
            <el-option
              v-for="item in groupOptions"
              :key="item"
              :label="item"
              :value="item"
            />
          </el-select>
        </el-form-item>
      </re-col>
      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="权限状态">
          <el-switch
            v-model="newFormInline.state"
            inline-prompt
            :active-value="true"
            :inactive-value="false"
            active-text="启用"
            inactive-text="停用"
            :style="switchStyle"
          />
        </el-form-item>
      </re-col>
    </el-row>
  </el-form>
</template>
